package com.study.www;

import com.study.www.config.IdWorks;
import com.study.www.config.RedisUtils;
import com.study.www.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    RedisUtils redisUtils;

    @Override
    public void run(String... args) throws Exception {
        try {
            Dept dept = new Dept(IdWorks.getId(), "测试", System.currentTimeMillis());
            redisUtils.set(dept.getId(), dept);
            // 从缓存中获取
            System.out.println("the reids ->" + ((Dept) redisUtils.get(dept.getId())));
            // 指定过期时间
            dept = new Dept(IdWorks.getId(), "测试过期时间", System.currentTimeMillis());
            redisUtils.set(dept.getId(), dept, 3L);
            System.out.println("the reids is validate ->" + ((Dept) redisUtils.get(dept.getId())));
            Thread.sleep(4000);
            System.out.println("the reids is not validate ->" + redisUtils.get(dept.getId()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
